/***************************************************************************
 *   Copyright (C) 2009 by Docteur Pierre-Marie Desombre.   *
 *   pm.desombre@medsyn.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 3 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "courbe.h"
#include "config.h"
#include <QtGui>
#include <QtCore>
#include <common/settings.h>
#include <common/position.h>
#include <common/constants.h>

enum Warn{WarnDebugMessage = true};
using namespace Constants;

Curses::Curses(QStandardItemModel * modelofdatas):m_modelofpatientdatas(0)
{
    setupUi(this);    
    setAttribute(Qt::WA_DeleteOnClose);
    settings set;
    int xPosition = set.value(X_POSITION).toInt();
    int yPosition = set.value(Y_POSITION).toInt();
    Position position;
    position.setPosition(this,xPosition,yPosition);
    scrollArea->setBackgroundRole(QPalette::Dark);  
    quitButton -> setText(trUtf8("&Quitter"));
    quitButton->setAutoDefault("true");
    m_SexeC      = modelofdatas->data(modelofdatas->index(0,SEX),Qt::DisplayRole).toString();
    m_modelofpatientdatas = modelofdatas;
    //slider
    zoomSlider->setRange(1,4); 
    m_paintingLabel = new PaintingLabel(scrollAreaWidgetContents);
    m_paintingLabel->getZoomAndSex(zoomSlider->value(),m_SexeC);
    gridLayout_3->addWidget(m_paintingLabel, 0, 0, 1, 1);
    //m_paintingLabel->show();
    //values doubleSpinBox
    const QString xLabelText = QString("<html><body><font size=3 color=blue>%1</font></body></html>")
                                 .arg(tr("months"));
    xLabel->setText(xLabelText);     
    const QString yLabelText = QString("<html><body><font size=3 color=blue>%1</font></body></html>")
                                 .arg("P/T²");
    yLabel->setText(yLabelText);
    xSpinBox->setMaximum(1000);
    ySpinBox->setMaximum(1000);     
    ySpinBox->hide();
    yLabel->hide();              
    displayCurses(0);

    //connect(comboBox,  SIGNAL(highlighted(int)),this,SLOT(displayCurses(int)));
    connect(quitButton,SIGNAL(pressed()),this,SLOT(quitCurseWidget()));
    connect(zoomSlider,SIGNAL(sliderMoved(int)),this,SLOT(setZoom(int)));
    connect(scrollArea->horizontalScrollBar () ,SIGNAL(valueChanged(int)),this,SLOT(setMonthValue(int)));
    connect(scrollArea->horizontalScrollBar () ,SIGNAL(valueChanged(int)),this,SLOT(displayCurses(int))); 
    connect(xSpinBox,SIGNAL(valueChanged(double)),this,SLOT(monthValueChanged(double)));
     
    connect(configButton,SIGNAL(pressed()),this,SLOT(showConfig()));
         
}//Curses constructeur

Curses::~Curses(){}

void Curses::quitCurseWidget()
{
    close();
}

double Curses::ageCalculatedInMonths(QDate & birthDate)
{
    QDate curDate = QDate::currentDate();
    int numberOfHisLifeDays = birthDate.daysTo(curDate);
    if (numberOfHisLifeDays == 0)
    {
    	  return 0;
        }
    return numberOfHisLifeDays/365;
}

void Curses::displayCurses(int month)
{   
    m_paintingpixmap.setDatas(m_modelofpatientdatas,m_SexeC,month);
    m_paintingpixmap.setRect(frame->width(),frame->height());
    m_paintingpixmap.paint();
    QPixmap pixmap = m_paintingpixmap.getPixmap();
    m_paintingLabel->getZoomAndSex(zoomSlider->value(),m_SexeC);
    m_paintingLabel->setPixmap(pixmap);    
}

void Curses::setZoom(int zoom)
{
    float fZoom = int(zoom);
    m_paintingpixmap.setDatas(m_modelofpatientdatas,m_SexeC,0);
    m_paintingpixmap.setRect(frame->width(),frame->height());
    m_paintingpixmap.setPixmapZoom(fZoom);
    m_paintingpixmap.paint();
    QPixmap pixmap = m_paintingpixmap.getPixmap();
    QString zoomText = QString("<html><body><font size=3 color=red>%1</font></body></font></html>")
                       .arg("Zoom "+QString::number(zoom)+" x");
    zoomLabel->setText(zoomText);
    m_paintingLabel->getZoomAndSex(zoom,m_SexeC);
    m_paintingLabel->setPixmap(pixmap);    
}

void Curses::resizeEvent(QResizeEvent * event)
{
    if (event->type() == QEvent::Resize)
    {
    	  int zoom = zoomSlider->value();
    	  setZoom(zoom);
        }        
}

/*void Curses::mouseMoveEvent(QMouseEvent * event)
{
    qDebug() << __FILE__ << QString::number(__LINE__) << " x = " << QString::number(event->pos().x()) 
                                                      << " y = " << QString::number(event->pos().y());
    int zoom = zoomSlider->value();
    int xWidget = event->pos().x();
    int yWidget = event->pos().y();
    int xFrame = m_paintingLabel->x();
    int yFrame = m_paintingLabel->y();
    //int xPixmap = m_paintingpixmap.getPixmap().x();
    double x = double((xWidget - xFrame -24 )/zoom );
    //double y = (frame->height() -  double((yWidget - yFrame -24 )))/zoom;
    double y = double((yWidget - yFrame  )/zoom );
    double bmi = getBmiWithCursor(y,zoom);
    qDebug() << __FILE__ << QString::number(__LINE__) << " getBmiWithCursor()  =" << QString::number(getBmiWithCursor(y,zoom)) ;
    qDebug() << __FILE__ << QString::number(__LINE__) << " BMI =" << QString::number(bmi) ;
    xSpinBox->setValue(x);
    ySpinBox->setValue(bmi -3);    
}*/

double Curses::getBmiWithCursor(double y,int zoom)
{
    float bmi = m_paintingpixmap.getBmi(y,zoom);
    return double(bmi);
}

void Curses::setMonthValue(int value)
{
    settings set(this);
    float widthMultiplicator = set.value(Constants::X_MULTIPLIER).toFloat();
    int month = value/widthMultiplicator; //FIXME
    xSpinBox->setValue(month);
}

void Curses::showConfig()
{
    ConfigDialog *cfg = new ConfigDialog(this);
    if (cfg->exec()==QDialog::Accepted)
    {
          cfg->record();
          QMessageBox::information(0,tr("Information"),tr("Le fichier de configuration a été modifié."),QMessageBox::Ok);
        }
    else
    {
        cfg->close();
        QMessageBox::information(0,tr("Information"),tr("Le fichier de configuration n'a pas été modifié."),QMessageBox::Ok);
        }
}

void Curses::monthValueChanged(double value)
{
    int valueint = int(value);//FIXME necessaire d'adapter value a decalage
    displayCurses(valueint);
}

